// pages/position/fullTime/detail/detail.js
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
let userId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    isCanRefresh: false,
    isShare: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id;

    this.setData({isShare: options.userId});
    // 通过分享进入，且无 token
    if (options.userId && !getApp().globalData.token){
      const params = {
        userId: options.userId,
        userType: 1
      }
      wx.redirectTo({
        url: '/pages/login/index?' + T.param(params)
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getDetail();
  },

  toast(text) {
    T.toast({ id: '#toast', text: text });
  },

  getDetail() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'position/' + this.data.id,
      method: 'GET',
      data: {
        token: token
      },
      success: (res) => {
        this.setData({obj: res});
        if ((res.checked == 1 && res.usable == 1) || (res.checked == 3 && res.usable == 1)) {
          this.setData({isCanRefresh: true})
        }
      }
    })
  },

  onRefresh() {
    let param =  {
      token: token,
      hrId: userId
    }
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'position/refresh/' + this.data.id + '?' + T.param(param),
      method: 'PUT',
      success: (res) => {
        this.toast('刷新成功');
      }
    })
  },

  onEdit() {
    let obj = this.data.obj;
    if (obj.temp == 0) {
      this.toast('专场职位不可编辑');
      return;
    }

    let nowTime = new Date().getTime();
    let deadline = String(obj.reward.deadline).length == 10 ? obj.reward.deadline * 1000 : obj.reward.deadline;
    if (nowTime < deadline) {
      this.toast('职位悬赏有效期内不可编辑');
      return;
    }

    if (obj.checked == 2 || obj.checked == 0) {
      this.toast('审核失败的职位不可编辑');
      return;
    }
    // 职位过期 不可编辑
    let param = {
      origin: 'edit',
      id: this.data.id
    }
    wx.navigateTo({
      url: '../add/add?' + T.param(param),
    })
  },

  onSearchResume() {
    let param = {
      id: this.data.id,
      type: 0
    }
    wx.navigateTo({
      url: '/pages/position/deliver/record?' + T.param(param),
    })
  },

  /**
   * 用户点击分享
   */
  onShareAppMessage: function (res) {
    const params = {
      id: this.data.id,
      userId: userId,
      userType: 1
    }
    return {
      title: this.data.obj.name,
      path: T.getCurrentPageUrl(false) + '?' + T.param(params),
      imageUrl: ''
    }
  }

})